package com.cms.clash.controller;


import cn.hutool.http.HtmlUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cms.clash.dto.ApiSaveDTO;
import com.cms.clash.dto.ArcQueryDTO;
import com.cms.clash.dto.ArcShowDTO;
import com.cms.clash.dto.TreeNode;
import com.cms.clash.entity.ArchiveContentEntity;
import com.cms.clash.entity.ArchiveEntity;
import com.cms.clash.entity.HostEntity;
import com.cms.clash.mapper.ArchiveContentMapper;
import com.cms.clash.mapper.ArchiveMapper;
import com.cms.clash.service.impl.ArchiveContentService;
import com.cms.clash.service.impl.ArchiveService;
import com.cms.clash.service.impl.ArchiveTypeService;
import com.cms.clash.util.CacheUtils;
import com.cms.clash.util.TemplateUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * ApiController
 * @author cms
 * @version 1.0.0
 * @date 2024-07-28
 */

@RestController
public class ApiController {
    private final static String ALLOW_TOKEN = "4fccd95e-2f99-4fd6-953c-9e35c7ba465b";
    @Autowired
    private ArchiveMapper archiveMapper;

    @Autowired
    private ArchiveContentMapper archiveContentMapper;

    @RequestMapping("/aaa_save_aaa")
    public String save(@RequestBody ApiSaveDTO dto, HttpServletRequest request) {
        String token = request.getParameter("token");
        if (!ALLOW_TOKEN.equals(token)){
            return null;
        }
        String uuid = TemplateUtil.makeRandUuid(8);
        LambdaQueryWrapper<ArchiveEntity> wrapper = new LambdaQueryWrapper<>();
        for (int i = 0; i < 100; i++) {
            wrapper.eq(ArchiveEntity::getUuid, uuid);
            Long num = archiveMapper.selectCount(wrapper);
            if (num == 0) {
                break;
            } else {
                wrapper.clear();
                uuid = TemplateUtil.makeRandUuid(8);
            }
        }

        ArchiveEntity entity = new ArchiveEntity();
        entity.setTitle(dto.getTitle());
        entity.setSeoTitle(dto.getTitle());
        entity.setSeoDescription(dto.getDescription());
        entity.setSeoKeywords(dto.getKeywords());
        entity.setTypeId(dto.getTypeId());
        entity.setThumb("");//1
        entity.setStatus(Byte.valueOf("1"));
        entity.setOrigin("clashfox");
        entity.setAuthor("clashfox");
        entity.setClick(new Random().nextInt(50, 200));
        entity.setUuid(uuid);
        Long time = System.currentTimeMillis() / 1000;
        entity.setCreateTime(time.intValue());
        int insert = archiveMapper.insert(entity);

        // 更新图片
        wrapper.clear();
        wrapper.eq(ArchiveEntity::getUuid, uuid);
        ArchiveEntity archiveEntity = archiveMapper.selectOne(wrapper);
        String imgNum = "0" + (archiveEntity.getAid() % 100);
        imgNum = (imgNum.length() < 3) ? "0" + imgNum : imgNum;
        String thumb = "/static/litpic/" + imgNum + ".webp";
        ArchiveEntity update = new ArchiveEntity();
        update.setAid(archiveEntity.getAid());
        update.setThumb(thumb);
        archiveMapper.updateById(update);

        // 更新详情
        ArchiveContentEntity contentEntity = new ArchiveContentEntity();
        contentEntity.setAid(archiveEntity.getAid());
        contentEntity.setContent(dto.getContent());
        int insert1 = archiveContentMapper.insert(contentEntity);
        Map<String, Object> map = new HashMap<>();
        if (insert1 > 0) {
            map.put("code", 200);
            map.put("msg", "保存成功");
            map.put("data", archiveEntity.getAid());
        } else {
            map.put("code", 500);
            map.put("msg", "保存失败");
            map.put("data", "");
        }
        return JSON.toJSONString(map);
    }


}
